<?php

namespace app\model;

use think\Model;

class OrderStaff extends Model
{

	protected $connection = 'mysql';

	protected $pk = 'id';

	protected $name = 'order_staff';

	public static function gettecid($order_id)
	{
		$staff = self::where('order_id', $order_id)->order('id desc')->find();
		return $staff->tecid;
	}
	public static function is_staff($order_id)
	{
		$staff = self::where('order_id', $order_id)->order('id desc')->find();
		return $staff->id;
	}

	public static function ordercount($id)
	{
		return self::where('id', $id)->count();
	}

	public static function staff($order_id)
	{
		$staff = self::where('order_id', $order_id)->order('id desc')->find();
		return Technical::getTitle($staff->tecid);
	}
	public static function checkstaff($technicalId, $selectDate, $begin_time, $end_time)
	{
		$ret = OrderStaff::where('yue_begin_time', '>=', strtotime($selectDate . ' ' . $begin_time . ':00'))
			->where('yue_end_time', '<=', strtotime($selectDate . ' ' . $end_time . ':00'))
			->where('id', $technicalId)
			->find();
		return $ret;
	}

	public static function getTechnical($order_id)
	{
		$staff = self::where('order_id', $order_id)->order('id desc')->find();
		return Technical::getInfo($staff->tecid);
	}

	public static function in_service($tecid)
	{
		if ($tecid) {
			$query = self::where(['tecid' => $tecid, 'is_complete' => 0]);

			$query->where('yue_begin_time', '<=', time());
			$query->where('yue_end_time', '>=', time());
			$order =  $query->find()?->toArray();
			if ($order) {
				return 1;
			}
		}
	}

	public static function is_technicalstaff($tecid, $yue_begin_time, $yue_end_time)
	{
		$Config = Config::getconfig('technical');
		if ($Config['is_multipleorder']) {
			if ($tecid) {
				$query = self::where(['tecid' => $tecid, 'is_complete' => 0]);
				if (!empty($yue_begin_time)) {
					$query->where('yue_begin_time', '<=', $yue_begin_time);
				}

				if (!empty($yue_begin_time)) {
					$query->where('yue_end_time', '>=', $yue_end_time);
				}
				$order =  $query->find()?->toArray();

				return $order;
			}
		}
	}

	public static function addstaff($order_staff)
	{

		if (self::is_technicalstaff($order_staff['tecid'], $order_staff['yue_begin_time'], $order_staff['yue_end_time'])) {
			return false;
		} else {
			$order =  self::where(['order_id' => $order_staff['order_id'], 'is_complete' => 0])->select()->toArray();
			if (!empty($order)) {
				self::where(['order_id' => $order_staff['order_id'], 'is_complete' => 0])->update(['tecid' => $order_staff['tecid']]);
			} else {
				$res = self::create($order_staff);
			}

			if (!empty($res)) {
				Broadcast::staff($order_staff);
				return $res;
			}
		}
	}
}
